package controller.admin;

import service.TypeService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * 用于删除指定的type类目
 */
@WebServlet(name = "AdminTypeDeleteServlet", value = "/admin/type_delete")
public class AdminTypeDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TypeService ts = new TypeService();
        String id = request.getParameter("id").trim();
        //id不为空
        if (!"".equals(id)) {
            //处理出错 防止瞎搞
            try {
                boolean flag = ts.deleteTypeById(Integer.parseInt(id));
                if (flag)
                    request.setAttribute("msg", "删除成功！");
                else
                    request.setAttribute("failMsg", "删除失败！");
            } catch (NumberFormatException e) {
                request.setAttribute("failMsg", "删除失败！");
                e.printStackTrace();
            }
        }
        request.getRequestDispatcher("type_list").forward(request, response);
    }
}
